package cn.talkimages.web.admin.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import cn.talkimages.framework.beans.PageArrayList;
import cn.talkimages.framework.poi.CellValueParser;
import cn.talkimages.framework.utils.page.PageUtil;
import cn.talkimages.web.admin.dao.ImageDAO;
import cn.talkimages.web.admin.service.ImageService;
import cn.talkimages.web.beans.ImageBean;
import cn.talkimages.web.service.impl.BaseServiceImpl;

public class ImageServiceImpl extends BaseServiceImpl implements ImageService {

	private static final Log LOG = LogFactory.getLog(ImageServiceImpl.class);

	private ImageDAO imageDAO;

	/**
	 * 登录图片信息
	 * 
	 * @param imageBaseInfo
	 * @param imageRelFile
	 */
	public void insertImages(ImageBean imageBaseInfo, File imageRelFile)
			throws Exception {

		Sheet imageRelSheet = createImageRelSheet(imageRelFile);

		int totalRows = imageRelSheet.getPhysicalNumberOfRows();

		// skip first row for titles
		for (int i = 1; i < totalRows; i++) {

			Row row = imageRelSheet.getRow(i);

			parseInsertImageRelRow(imageBaseInfo, row);

			imageDAO.insertImage(imageBaseInfo);
		}
	}

	/**
	 * 
	 */
	public List<ImageBean> getImageInfos(Map<String, Object> params,
			Integer pageNo, Integer pageSize) throws Exception {

		Long total = imageDAO.selectImageInfosCount(params);

		PageArrayList<ImageBean> imagePageList;

		// 有比配图片数据
		if (total > 0) {

			params.put("offset", PageUtil.getOffset(pageNo, pageSize));
			params.put("size", pageSize);

			// 取得图片信息
			List<ImageBean> images = imageDAO.selectImageInfos(params);

			imagePageList = new PageArrayList<ImageBean>(images);
		} else {
			imagePageList = new PageArrayList<ImageBean>();
		}

		imagePageList.setNumFound(total);
		imagePageList.setPageNo(pageNo);
		imagePageList.setPageSize(pageSize);

		return imagePageList;
	}

	// --------------------- Image Excel Process --------------------- //

	/**
	 * analyze the excel data and set to image bean.
	 * 
	 * @param imageBaseInfo
	 * @param row
	 * @throws Exception
	 */
	protected void parseInsertImageRelRow(ImageBean imageBaseInfo, Row row)
			throws Exception {

		Cell cell;

		// 图片前缀
		cell = row.getCell(0);
		String noPrefix = CellValueParser.getStringValue(cell);
		imageBaseInfo.setNoPrefix(noPrefix);

		// 图片编号
		cell = row.getCell(1);
		long no = CellValueParser.getLongValue(cell);
		imageBaseInfo.setNo(no);

		// 原始图片编号
		cell = row.getCell(2);
		String originNo = CellValueParser.getStringValue(cell);
		imageBaseInfo.setOriginNo(originNo);

		// 图片路径
		cell = row.getCell(3);
		String dir = CellValueParser.getStringValue(cell);
		imageBaseInfo.setDir(dir);
	}

	/**
	 * get excel sheet
	 * 
	 * @param imageRelFile
	 * @return Sheet
	 * @throws Exception
	 */
	protected Sheet createImageRelSheet(File imageRelFile) throws Exception {

		FileInputStream fis = null;

		try {

			fis = new FileInputStream(imageRelFile);

			Workbook xlsWorkbook = WorkbookFactory.create(fis);

			if (xlsWorkbook.getNumberOfSheets() == 0) {
				throw new IllegalStateException(
						"there is no sheet in xls file.");
			}

			return xlsWorkbook.getSheetAt(0);

		} finally {
			if (null != fis) {
				try {
					fis.close();
				} catch (IOException ioe) {
					LOG.warn("fail to close xls filestream.", ioe);
				}
			}
		}

	}

	// -------------------------- Getter & Setter -------------------------- //

	public ImageDAO getImageDAO() {
		return imageDAO;
	}

	public void setImageDAO(ImageDAO imageDAO) {
		this.imageDAO = imageDAO;
	}

}
